home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewControl-p / Buggy C Port / CONTROL.c next >
Encoding:
Text File  |  1994-12-04  |  17.5 KB  |  647 lines  |  [TEXT/KAHL]

  1.  
  2. //• C translation from Pascal source file: CONTROL.p
  3.  
  4. //• $X-.
  5.    //• Turn off stack expansion. This is a Lisa concept, not needed on Mac.
  6. //• $U-.
  7.    //• Turn off the Lisa Libraries. This is required by the WorkShop.
  8. //• $R-.
  9.    //• Turn off range checking.
  10.  
  11. //• main ScrollControls;
  12.  
  13. //• Jeffery J. Bradford    Macintosh Technical Support  April 1985 .
  14.  
  15. //• This example will help you become familiar with how scroll bars work.
  16. //• and allow you manipulate the contents of a window. This particular  .
  17. //• example remembers where the drawing origin should be and sets the   .
  18. //• origin to that spot before drawing every time. This involves some   .
  19. //• book keeping, but we've made it pretty simple and minimized globals..
  20.  
  21.  
  22.     //• includes:
  23.         Types, QuickDraw, OSUtils, ToolUtils, Packages;
  24.  
  25.     enum {
  26. //• menu stuff.
  27.         AppleMenu == 256;
  28.         FileMenu == 257;
  29.         EditMenu == 258;
  30.  
  31. //• window IDs.
  32.         WindResID == 256;
  33.  
  34. //• scroll bar res IDs.
  35.         VScrollBar == 256;      //• res ID for bottom/horz scroll bar.
  36.         HScrollBar == 257;      //• res ID for side/vert scroll bar.
  37.  
  38. //• control stuff.
  39.         didntFindaControl == 0;     //• used in IF statements for clarity.
  40.         didntMove == 0;     //• used in IF statements for clarity.
  41.         VSBarWidth == 15;    //• width of vertical scroll bar.
  42.         HSBarHeight == 15;    //• width of horz. scroll bar.
  43.         Line_Inc == 5;     //• amount of scrolling per button press.
  44.         Page_Inc == 100;   //• amount of scrolling per page press.
  45.         HorzScrollBar == 1;     //• used to distinguish between vert & horz scroll bar.
  46.         VertScrollBar == 0;     //• used to distinguish between vert & horz scroll bar.
  47.  
  48.  
  49.  
  50.     
  51. //• this is useful stuff you might need sometime.
  52.  
  53.          typedef //• packed//• Expected ;.
  54.  WordStuff;
  55.                 //• Expected type identifier.
  56.  //• Expected =.
  57. typedef //• Expected type.
  58. //• Expected ;.
  59.  switch (;
  60.                     //• Expected =.
  61. typedef enum {
  62.                             Word: short
  63.                     } 0;
  64.                     //• Expected =.
  65. typedef enum {
  66.                             SByte1, SByte0: SignedByte
  67.                     } 1;
  68.                     //• Expected =.
  69. typedef enum {
  70.                             b15, b14, b13, b12, b11, b10, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0: Boolean
  71.                     //• Expected ;.
  72. }//• Expected ;.
  73.  2;//• Expected type identifier.
  74.  
  75.  
  76.         //• Expected =.
  77. typedef //• Expected type.
  78. //• Expected ;.
  79.  ;;  //• Expected =.
  80. typedef chr3//• Expected ;.
  81.  //• packed; //• Expected =.
  82. typedef chr1//• Expected ;.
  83.  chr2; //• Expected =.
  84. typedef char chr0;
  85.             };
  86.  
  87.         LMwordPtr == [0]short;          //• pointer to low memory address.
  88.         LMLongPtr == [0]long;          //• pointer to low memory address - long.
  89.  
  90.  
  91.  
  92.     
  93. //• global program stuff.
  94.         Boolean Finished;    //• used to terminate the program.
  95.         CursHandle ClockCursor; //• handle to the waiting watch cursor.
  96.  
  97. //• Screen stuff.
  98.         Rect DragArea;          //• holds the area where window can be dragged in.
  99.         Rect GrowArea;          //• holds the area to which a window's size can change.
  100.         Rect Screen;          //• holds the screen dimensions.
  101.  
  102. //• Window stuff.
  103.         WindowPtr OneWindow;     //• pointer to the first window.
  104.         Rect UsableArea;          //• uasable area of the windows content region.
  105.  
  106. //• -----------------------------------------------------------------------------.
  107. //•                      end of global variable definition.
  108. //• -----------------------------------------------------------------------------.
  109.  
  110.     void DrawBullseye()
  111. {
  112.  
  113.             Rect TempRect;
  114.             short i, a, b, x;
  115.     {
  116.         MoveTo(0, 0);
  117.         LineTo(700, 700);
  118.         MoveTo(0, 350);
  119.         LineTo(700, 350);
  120.         MoveTo(0, 700);
  121.         LineTo(700, 0);
  122.         MoveTo(350, 0);
  123.         LineTo(350, 700);
  124.  
  125.         a = 0;
  126.         b = 700;
  127.         for ( i = 0 ; • < 11 )
  128.             {
  129.                 x = 56 - i * 5;
  130.                 a = a + x;
  131.                 b = b - x;
  132.                 SetRect(TempRect, a, a, b, b);
  133.                 FrameRect(TempRect);
  134.             };
  135.  
  136.         FillRect(TempRect, black);
  137.     };
  138.  
  139. //• -----------------------------------------------------------------------------.
  140.  
  141.     void DrawDisplay()
  142. {
  143.  
  144.             Rect TempRect;  //• used for offseting the Usable display area.
  145.             Point CurOrigin; //• holds the current origin.
  146.  
  147.     {
  148. //• in order to confine the drawing to the usable content area of the window.
  149. //• we will set a clipRect of that area.
  150.  
  151.         TempRect = UsableArea;
  152.         CurOrigin = Point(GetWRefCon(OneWindow));     //• get the current origin.
  153.  
  154. //• set up to draw properly, and change the clip.
  155.         SetOrigin(CurOrigin.h, CurOrigin.v);           //• account for scrolling.
  156.         OffsetRect(TempRect, CurOrigin.h, CurOrigin.v); //• account for scrolling.
  157.         ClipRect(TempRect);
  158.  
  159.         DrawBullseye;
  160.  
  161.         SetOrigin(0, 0);                   //• reset it back to normal.
  162.         ClipRect(OneWindow->portRect);   //• reset it to the original window.
  163.     };
  164.  
  165. //• -----------------------------------------------------------------------------.
  166.  
  167.     void ScrolltheScreen(ControlHandle cntlHdl)
  168. {
  169.  
  170.             Point Origin;     //• holds the coordinates of the new & old origin.
  171.             Point OldOrigin;      //• holds the coordinates of the old/last origin.
  172.             Point NewOrigin;      //• holds the coordinates of the new origin.
  173.             short delta_H;
  174.             short delta_V;
  175.             RgnHandle UpDateRgn;  //• holds the region to be scrolled.
  176.  
  177.     {
  178.  
  179. //•   Code below is for clarity}
  180. {{get the current (or old) origin}
  181. {OldOrigin := Point(GetWRefCon(OneWindow));}
  182.  
  183. {get the NEW origin, we can only scroll in one direction at a time so}
  184. {if GetCRefCon(C
  185.  
  186.         Origin = Point(GetWRefCon(OneWindow));
  187.         if ( GetCRefCon(CntlHdl) == VertScrollBar )
  188.             {
  189.                 delta_H = 0;                                //• didn't change.
  190.                 delta_V = Origin.v - GetCtlValue(CntlHdl);  //• get the diff.
  191.                 Origin.v = GetCtlValue(CntlHdl);             //• set to new position.
  192.             }
  193.         else
  194.             {
  195.                 delta_V = 0;                                //• didn't change.
  196.                 delta_H = Origin.h - GetCtlValue(CntlHdl);  //• get the diff.
  197.                 Origin.h = GetCtlValue(CntlHdl);             //• set to new position.
  198.             };
  199.  
  200.  
  201. //• get a region for use in updating/scrolling the screen.
  202.         UpdateRgn = NewRgn ();
  203.         ScrollRect(UsableArea, delta_H, delta_V, updateRgn);
  204.  
  205.         SetOrigin(Origin.h, Origin.v);  //• set to the new origin.
  206.  
  207. //• move the update Rgn to new location.
  208.         OffsetRect(updateRgn[0]->rgnBBox, Origin.h, Origin.v);
  209.  
  210. //• clip to it.
  211.         ClipRect(updateRgn[0]->rgnBBox);
  212.  
  213.         DrawBullseye;         //• do the drawing.
  214.  
  215. //• reset everything back to normal & save the new origin.
  216.         SetOrigin(0, 0);
  217.         ClipRect(OneWindow->portRect);
  218.         SetWRefCon(OneWindow, long(Origin));
  219.  
  220.         DisposeRgn(updateRgn);
  221.     };
  222.  
  223. //• -----------------------------------------------------------------------------.
  224.  
  225.     void LineScroll(ControlHandle SBar, short ControlLoc)
  226. {
  227. {
  228.         if ( ControlLoc == inUpButton )
  229.             SetCtlValue(SBar, GetCtlValue(SBar) - Line_inc)
  230.         else
  231.             SetCtlValue(SBar, GetCtlValue(SBar) + Line_inc);
  232.  
  233.         ScrolltheScreen(SBar);
  234.     };
  235.  
  236. //• -----------------------------------------------------------------------------.
  237.  
  238.     void HandleControl_at(Point atMouseLoc, WindowPtr inThisWindow)
  239. {
  240.  
  241.             ControlHandle CntlHdl;  //• handle of a control part selected.
  242.             short ControlLoc;        //• position ID of the control part selected.
  243.             short dummy;        //• used to hold integer returned from TrackControl.
  244.  
  245.     {
  246.         ControlLoc = FindControl(atMouseLoc, inThisWindow, CntlHdl);
  247.  
  248.         if ( ControlLoc != didntFindaControl )
  249.             switch ( ControlLoc )
  250.             {
  251.  
  252.                 inUpButton: 
  253.                     dummy = TrackControl(CntlHdl, atMouseLoc, @LineScroll);
  254.  
  255.                 inDownButton: 
  256.                     dummy = TrackControl(CntlHdl, atMouseLoc, @LineScroll);
  257.  
  258.                 inPageUp: 
  259.                     {
  260.                         SetCtlValue(CntlHdl, GetCtlValue(CntlHdl) - Page_inc);
  261.                         ScrolltheScreen(CntlHdl);
  262.                     };
  263.  
  264.                 inPageDown: 
  265.                     {
  266.                         SetCtlValue(CntlHdl, GetCtlValue(CntlHdl) + Page_inc);
  267.                         ScrolltheScreen(CntlHdl);
  268.                     };
  269.  
  270.                 inThumb: 
  271.                     if ( TrackControl(CntlHdl, atMouseLoc, 0L) != didntMove )
  272.                         ScrolltheScreen(CntlHdl);
  273.  
  274.             };//• case of control location.
  275.     };//• of Handle Window Controls.
  276.  
  277. //• -----------------------------------------------------------------------------.
  278.  
  279.     void ProcessMenu_in(long CodeWord)
  280. {
  281.  
  282.             short Menu_No;        //• menu number that was selected.
  283.             short Item_No;        //• item in menu that was selected.
  284.             Str255 NameHolder;         //• name holder for desk accessory or font.
  285.             short DNA;        //• OpenDA will never return 0, so don't care.
  286.  
  287.     {
  288.         if ( CodeWord != 0 ) //• go ahead and process the command.
  289.             {
  290.                 Menu_No = HiWord(CodeWord);   //• get the Hi word of....
  291.                 Item_no = LoWord(CodeWord);   //• get the Lo word of....
  292.  
  293.                 switch ( Menu_No )
  294.             {
  295.  
  296.                     AppleMenu: 
  297.                         {
  298.                             GetItem(GetMHandle(AppleMenu), Item_No, NameHolder);
  299.                             DNA = OpenDeskAcc(NameHolder);
  300.                         };
  301.  
  302.                     FileMenu: 
  303.                         {
  304.                             switch ( Item_No )
  305.             {
  306.                                 1: 
  307.                                     Finished = true;          //• quit.
  308.                             };
  309.                         };
  310.  
  311.                     EditMenu: 
  312.                         {
  313.                             if ( ! SystemEdit(Item_no - 1) ) //• if not for a desk accessory.
  314.                                 switch ( Item_No )
  315.             {
  316.                                     1: 
  317.                                         {
  318.                                         ;};               //• undo.
  319.                     //•  2:                           line divider.
  320.                                     3: 
  321.                                         {
  322.                                         ;};               //• cut.
  323.                                     4: 
  324.                                         {
  325.                                         ;};               //• copy.
  326.                                     5: 
  327.                                         {
  328.                                         ;};               //• paste.
  329.                                     6: 
  330.                                         {
  331.                                         ;};               //• clear.
  332.                                 };
  333.                         };
  334.  
  335.                 };//• case of Menu_No.
  336.  
  337.                 HiliteMenu(0);               //• unhilite after processing menu.
  338.             }; //• the If codeword <> 0.
  339.     }; //• of ProcessMenu_in procedure.
  340.  
  341.  
  342. //• -------------------------------------------------------------------.
  343. //• ----- These are procedures called from the main event loop  -------.
  344.  
  345.     void DealwthMouseDowns(EventRecord Event)
  346. {
  347.  
  348.             WindowPtr WindowPointedTo;
  349.             Point MouseLoc;
  350.             short WindoLoc;
  351.     {
  352.         MouseLoc = Event.Where;
  353.         WindoLoc = FindWindow(MouseLoc, WindowPointedTo);
  354.         switch ( WindoLoc )
  355.             {
  356.  
  357.             inMenuBar: 
  358.                 ProcessMenu_in(MenuSelect(MouseLoc));
  359.  
  360.             inSysWindow: 
  361.                 SystemClick(Event, WindowPointedTo);
  362.  
  363.             inContent: 
  364.                 if ( WindowPointedTo != FrontWindow () )
  365.                     SelectWindow(WindowPointedTo)
  366.                 else
  367.                     {
  368.                         GlobaltoLocal(MouseLoc);
  369.                         if ( ! PtinRect(MouseLoc, UsableArea) )
  370.                             HandleControl_at(MouseLoc, WindowPointedTo)
  371.                         else
  372.                             InvalRect(UsableArea);  //• just for effect.
  373.                     };
  374.  
  375.             inGrow: 
  376.                 if ( WindowPointedTo != FrontWindow () )
  377.                     SelectWindow(WindowPointedTo)
  378.                 else
  379.                     { //• ReSizeWindow(WindowPointedTo,MouseLoc);.
  380.                     ;};
  381.  
  382.             inDrag: 
  383.                 DragWindow(WindowPointedTo, MouseLoc, DragArea);
  384.  
  385.             inGoAway: 
  386.                 if ( TrackGoAway(WindowPointedTo, MouseLoc) )
  387.                     DisposeWindow(WindowPointedTo); //• since W mgr allocated space.
  388.  
  389.         };//•  of case.
  390.     };
  391.  
  392. //• -----------------------------------------------------------------------------.
  393.  
  394.     void DealwthKeyDowns(EventRecord Event)
  395. {
  396.  
  397.             char CharCode;
  398.     {
  399.         CharCode = CharStuff(Event.message).Chr0; //• get low byte w/no processing.
  400.  
  401.         if ( (Event.message, cmdKey) == cmdKey )
  402.             {  //• key board command - probably a menu command.
  403.                 ProcessMenu_in(MenuKey(CharCode));
  404.             }
  405.         else
  406.             {
  407.        //• regular keyboard entry.
  408.             ;};
  409.     };
  410.  
  411. //• -----------------------------------------------------------------------------.
  412.  
  413.     void DealwthActivates(EventRecord Event)
  414. {
  415.  
  416.             WindowPtr TargetWindow;
  417.             WindowPeek TargetPeek;
  418.             ControlHandle CntrlHdl;
  419.     {
  420.         TargetWindow = WindowPtr(Event.message);
  421.         TargetPeek = WindowPeek(TargetWindow);
  422.         DrawGrowIcon(TargetWindow);
  423.  
  424.         if ( Odd(Event.modifiers) )
  425.  
  426.  //• the window is becoming active.
  427.             {
  428. //• 1.
  429.                 SetPort(TargetWindow);
  430.  
  431. //• 2.
  432.     //• activate the window controls.
  433.                 CntrlHdl = ControlHandle(TargetPeek->ControlList);  //• get the first one.
  434.                 do
  435.         {
  436.                     if ( CntrlHdl != 0L )   //• in case we get a nil on the first one.
  437.                         {
  438.                             ShowControl(CntrlHdl);
  439.                             CntrlHdl = CntrlHdl[0]->nextControl;
  440.                         };
  441.                 } while (!  CntrlHdl == 0L;
  442.  
  443. //• 3.
  444.    //• activate selections, etc..
  445.             }
  446.  
  447.  
  448.         else //• the window is being DEactivatived.
  449.             {
  450. //• 1.
  451.    //• DEactivate the window controls.
  452.                 CntrlHdl = ControlHandle(TargetPeek->ControlList);  //• get the first one.
  453.                 do
  454.         {
  455.                     if ( CntrlHdl != 0L )   //• in case we get a nil on the first one.
  456.                         {
  457.                             HideControl(CntrlHdl);
  458.                             CntrlHdl = CntrlHdl[0]->nextControl;
  459.                         };
  460.                 } while (!  CntrlHdl == 0L;
  461.  
  462. //• 2.
  463.     //• deactivate selections, etc.
  464.             };
  465.     };
  466.  
  467. //• -----------------------------------------------------------------------------.
  468.  
  469.     void DealwthUpdates(EventRecord Event)
  470. {
  471.  
  472.             WindowPtr UpDateWindow, TempPort;
  473.     {
  474.         UpDateWindow = WindowPtr(Event.message);
  475.         GetPort(TempPort);                //• Save the current port.
  476.  
  477.         SetPort(UpDateWindow);      //• set the port to one in Evt.msg.
  478.         BeginUpdate(UpDateWindow);
  479.         EraseRect(UsableArea);
  480.         DrawDisplay;
  481.         DrawControls(UpDateWindow);
  482.         DrawGrowIcon(UpDateWindow);
  483.         EndUpdate(UpDateWindow);
  484.  
  485.         SetPort(TempPort);             //• restore to the previous port.
  486.     };
  487.  
  488. //• -----------------------------------------------------------------------------.
  489.  
  490.     void MainEventLoop()
  491. {
  492.  
  493.             EventRecord Event;
  494.             Boolean ProcessIt;
  495.     {
  496.         do
  497.         {
  498.             SystemTask ();             //• so you can support Desk Accessories.
  499.  
  500.             ProcessIt = GetNextEvent(everyEvent, Event);
  501.             if ( ProcessIt )//• is true.
  502.  //• we'll ProcessIt.
  503.                 switch ( Event.what )
  504.             {
  505.  
  506.                     mouseDown: 
  507.                         DealwthMouseDowns(Event);
  508.                     keyDown: 
  509.                         DealwthKeyDowns(Event);
  510.                     ActivateEvt: 
  511.                         DealwthActivates(Event);
  512.                     updateEvt: 
  513.                         DealwthUpdates(Event);
  514.  
  515.                 };//• of Case.
  516.         } while (!  Finished; //• terminate the program.
  517.     };
  518.  
  519. //• -----------------------------------------------------------------------------.
  520.  
  521.     void InitThings()
  522. {
  523. {
  524.         InitGraf(@thePort);          //• create a grafport for the screen.
  525.  
  526.         MoreMasters ();                 //• extra pointer blocks at the bottom of the heap.
  527.         MoreMasters ();                 //• this is 5 X 64 master pointers.
  528.         MoreMasters ();
  529.         MoreMasters ();
  530.         MoreMasters ();
  531.  
  532. //• get the cursors we use and lock them down - no clutter.
  533.         ClockCursor = GetCursor(watchCursor);
  534.         HLock(Handle(ClockCursor));
  535.  
  536. //• show the watch while we wait for inits & setups to finish.
  537.         SetCursor(ClockCursor[0][0]);
  538.  
  539. //• init everything in case the app is the Startup App.
  540.         InitFonts ();                     //• startup the fonts manager.
  541.         InitWindows ();                   //• startup the window manager.
  542.         InitMenus ();                     //• startup the menu manager.
  543.         TEInit ();                        //• startup the text edit manager.
  544.         InitDialogs(0L);              //• startup the dialog manager.
  545.  
  546.         Finished = false;             //• set program terminator to false.
  547.         FlushEvents(everyEvent, 0);     //• clear events from previous program.
  548.     };
  549.  
  550. //• -----------------------------------------------------------------------------.
  551.  
  552.     void SetupLimits()
  553. {
  554. {
  555.         Screen = ScreenBits.Bounds;   //• set the size of the screen.
  556.         SetRect(DragArea, Screen.left + 4, Screen.top + 24, Screen.right - 4, Screen.bottom - 4);
  557.         SetRect(GrowArea, Screen.left, Screen.top + 24, Screen.right, Screen.bottom);
  558.     };
  559.  
  560. //• -----------------------------------------------------------------------------.
  561.  
  562.     void SetupMenus()
  563. {
  564.  
  565.             MenuHandle MenuTopic;
  566.     {
  567.         MenuTopic = GetMenu(AppleMenu);  //• get the apple desk accessories menu.
  568.         AddResMenu(MenuTopic, "DRVR");     //• adds all names into item list.
  569.         InsertMenu(MenuTopic, 0);          //• put in list held by menu manager.
  570.  
  571.         MenuTopic = GetMenu(FileMenu);   //• always need this for Quiting.
  572.         InsertMenu(MenuTopic, 0);
  573.  
  574.         MenuTopic = GetMenu(EditMenu);   //• always need for editing Desk Accessories.
  575.         InsertMenu(MenuTopic, 0);
  576.  
  577.         DrawMenuBar ();           //• all done so show the menu bar.
  578.     };
  579.  
  580. //• -----------------------------------------------------------------------------.
  581.  
  582.     void SetupWindows()
  583. {
  584.  
  585.             ControlHandle vScroll;  //• used to create control.
  586.             ControlHandle hScroll;  //• same thing, we could put them in global but the.
  587.                                //• the window is global and it will have them.
  588.     {
  589.         OneWindow = GetNewWindow(WindResID, 0L, Ptr(-1));
  590.         SetPort(OneWindow);  //• so usableArea is correct.
  591.  
  592. //• now setup the usable window area.
  593.         UsableArea = OneWindow->portRect;                           //• initialize it.
  594.         UsableArea.right = UsableArea.right - VSBarWidth;
  595.         UsableArea.bottom = UsableArea.bottom - HSBarHeight;
  596.  
  597. //• now set up the scroll bar controls that this window will use.
  598.         vScroll = GetNewControl(VScrollBar, OneWindow);  //• get vertical scroll bar.
  599.         hScroll = GetNewControl(HScrollBar, OneWindow);  //• get horizontal scroll bar.
  600.  
  601. //• our display is going to be 700 by 700 pixels so set min & max values.
  602. //• Note: the window is 290 by 490.
  603.         SetCtlMin(hScroll, 0);         //• set the minimum value in vertical direction.
  604.         SetCtlMax(hScroll, 210);       //• set the maximum value in vertical direction.
  605.         SetCtlMin(vScroll, 0);         //• set the minimum value in horizontal direction.
  606.         SetCtlMax(vScroll, 410);       //• set the maximum value in horizontal direction.
  607.  
  608. //• the current origin will be stored in the refcon of the window.
  609. //•   NOTE: its already been set in the resource file}
  610. {SetWRefCon(OneWindow, 0);             initialize it to zero*.
  611.  
  612. //• all set so show the window.
  613.         ShowWindow(OneWindow);
  614.  
  615. //• Note that the controls are brought in invisibily, show them.
  616. //• when the window becomes active, hide them when its inactive.
  617.     };
  618.  
  619. //• -----------------------------------------------------------------------------.
  620.  
  621.     void SetUpThings()
  622. {
  623. {
  624.         SetupWindows;         //• do first so its low in heap.
  625.         SetupMenus;
  626.         SetupLimits;
  627.  
  628.         InitCursor ();           //• ready to go, so show the Arrow cursor.
  629.     };
  630.  
  631. //• -----------------------------------------------------------------------------.
  632.  
  633.     void CloseThings()
  634. {
  635. {
  636. //• close files, if you changed sys resources, UNchange them here be carefull.
  637. //• about changing sys things, remember the Switcher could be around.
  638.     ;};
  639.  
  640. //• -----------------------------------------------------------------------------.
  641.  
  642. {
  643.     InitThings;
  644.     SetUpThings;
  645.     MainEventLoop;
  646.     CloseThings;
  647. end.